var w = 0;
Ext.define('AM.view.grid.BudgetGrid' ,{
    extend: 'Ext.grid.Panel',
    alias: 'widget.budgetGrid',
    id: 'budgetGrid',
    store: 'StoreBudgetGrid',
    title: 'Buget',
    
    initComponent: function() {
    var me = this;
        this.columns = [
            {xtype: 'rownumberer',flex: .0},
            {header: 'Categorie',dataIndex:'cild',flex: 4, summaryType: 'count'},
            {header: 'Jan', dataIndex: 'Jan', flex: 4,
            summaryType: function(field){
                me = this;
                var store = Ext.getStore('StoreBudgetGrid');
                var records = store.data.items;
                var field = ['Feb'];
                function Suma(record, field) {
                    var total = 0;
                    var j = 0,
                    lenn = record.length;
                    for (; j < lenn; ++j) {
                       total = total + parseFloat(record[j].get(field));
                    }
                    return total.toFixed(2);
                };
                if (store.isGrouped()) {
                    var groups = store.getGroups();
                    var i = 0;
                    var len = groups.length;
                    var out = {},
                    group;
                    for (; i < len; i++) {
                        group = groups[i];
                        out[group.name] = Suma.apply(store, [group.children].concat(field));
                    }
                    var groupSum = out[groups[w].name];
                    w++;
                    return groupSum;
                } else {
                    return Suma.apply(store, [records].concat(field));
                }
            },
          summaryRenderer: function(value, summaryData, dataIndex) {
              return '<b>' + value + '</b>';
          }
            },
            {header: 'Feb', dataIndex: 'Feb', flex: 4,
            summaryType: function(field){
                    me = this;
                    var store = Ext.getStore('StoreBudgetGrid');
                    var records = store.data.items;
                    var field = ['Feb'];
                    function Suma(record, field) {
                        var total = 0;
                        var j = 0,
                        lenn = record.length;
                        for (; j < lenn; ++j) {
                           total = total + parseFloat(record[j].get(field));
                        }
                        return total.toFixed(2);
                    };
                    if (store.isGrouped()) {
                        var groups = store.getGroups();
                        var i = 0;
                        var len = groups.length;
                        if(w > len -1){
                            w = 0;
                        }
                        var out = {},
                        group;
                        for (; i < len; i++) {
                            group = groups[i];
                            out[group.name] = Suma.apply(store, [group.children].concat(field));
                        }
                        var groupSum = out[groups[w].name];
                        w++;
                        return groupSum;
                    } else {
                        return Suma.apply(store, [records].concat(field));
                    }
                },
              summaryRenderer: function(value, summaryData, dataIndex) {
                  return '<b>' + value + '</b>';
              }
              },
            {header: 'Mar', flex: 4, dataIndex: 'Mar',
                summaryType: function(field){
                      me = this;
                      var store = Ext.getStore('StoreBudgetGrid');
                      var records = store.data.items;
                      var field = ['Mar'];
                      function Suma(record, field) {
                          var total = 0;
                          var j = 0,
                          lenn = record.length;
                          for (; j < lenn; ++j) {
                             total = total + parseFloat(record[j].get(field));
                          }
                          return total.toFixed(2);
                      };
                      if (store.isGrouped()) {
                          var groups = store.getGroups();
                          var i = 0;
                          var len = groups.length;
                          if(w > len -1){w = 0;}
                          var out = {},
                          group;
                          for (; i < len; i++) {
                              group = groups[i];
                              out[group.name] = Suma.apply(store, [group.children].concat(field));
                          }
                          var groupSum = out[groups[w].name];
                          w++;
                          return groupSum;
                      } else {
                          return Suma.apply(store, [records].concat(field));
                      }
                  },
                summaryRenderer: function(value, summaryData, dataIndex) {
                    return '<b>' + value + '</b>';
                }
            },
            {header: 'Apr', flex: 4, dataIndex: 'Apr',
                summaryType: function(field){
                      me = this;
                      var store = Ext.getStore('StoreBudgetGrid');
                      var records = store.data.items;
                      var field = ['Apr'];
                      function Suma(record, field) {
                          var total = 0;
                          var j = 0,
                          lenn = record.length;
                          for (; j < lenn; ++j) {
                             total = total + parseFloat(record[j].get(field));
                          }
                          return total.toFixed(2);
                      };
                      if (store.isGrouped()) {
                          var groups = store.getGroups();
                          var i = 0;
                          var len = groups.length;
                          if(w > len -1){w = 0;}
                          var out = {},
                          group;
                          for (; i < len; i++) {
                              group = groups[i];
                              out[group.name] = Suma.apply(store, [group.children].concat(field));
                          }
                          var groupSum = out[groups[w].name];
                          w++;
                          return groupSum;
                      } else {
                          return Suma.apply(store, [records].concat(field));
                      }
                  },
                summaryRenderer: function(value, summaryData, dataIndex) {
                    return '<b>' + value + '</b>';
                }
            },
            {header: 'May', flex: 4, dataIndex: 'May',
                summaryType: function(field){
                      me = this;
                      var store = Ext.getStore('StoreBudgetGrid');
                      var records = store.data.items;
                      var field = ['May'];
                      function Suma(record, field) {
                          var total = 0;
                          var j = 0,
                          lenn = record.length;
                          for (; j < lenn; ++j) {
                             total = total + parseFloat(record[j].get(field));
                          }
                          return total.toFixed(2);
                      };
                      if (store.isGrouped()) {
                          var groups = store.getGroups();
                          var i = 0;
                          var len = groups.length;
                          if(w > len -1){w = 0;}
                          var out = {},
                          group;
                          for (; i < len; i++) {
                              group = groups[i];
                              out[group.name] = Suma.apply(store, [group.children].concat(field));
                          }
                          var groupSum = out[groups[w].name];
                          w++;
                          return groupSum;
                      } else {
                          return Suma.apply(store, [records].concat(field));
                      }
                  },
                summaryRenderer: function(value, summaryData, dataIndex) {
                    return '<b>' + value + '</b>';
                }
            },
            {header: 'Jun', flex: 4, dataIndex: 'Jun',
                summaryType: function(field){
                      me = this;
                      var store = Ext.getStore('StoreBudgetGrid');
                      var records = store.data.items;
                      var field = ['Jun'];
                      function Suma(record, field) {
                          var total = 0;
                          var j = 0,
                          lenn = record.length;
                          for (; j < lenn; ++j) {
                             total = total + parseFloat(record[j].get(field));
                          }
                          return total.toFixed(2);
                      };
                      if (store.isGrouped()) {
                          var groups = store.getGroups();
                          var i = 0;
                          var len = groups.length;
                          if(w > len -1){w = 0;}
                          var out = {},
                          group;
                          for (; i < len; i++) {
                              group = groups[i];
                              out[group.name] = Suma.apply(store, [group.children].concat(field));
                          }
                          var groupSum = out[groups[w].name];
                          w++;
                          return groupSum;
                      } else {
                          return Suma.apply(store, [records].concat(field));
                      }
                  },
                summaryRenderer: function(value, summaryData, dataIndex) {
                    return '<b>' + value + '</b>';
                }
            },
            {header: 'Jul', flex: 4, dataIndex: 'Jul',
                summaryType: function(field){
                      me = this;
                      var store = Ext.getStore('StoreBudgetGrid');
                      var records = store.data.items;
                      var field = ['Jul'];
                      function Suma(record, field) {
                          var total = 0;
                          var j = 0,
                          lenn = record.length;
                          for (; j < lenn; ++j) {
                             total = total + parseFloat(record[j].get(field));
                          }
                          return total.toFixed(2);
                      };
                      if (store.isGrouped()) {
                          var groups = store.getGroups();
                          var i = 0;
                          var len = groups.length;
                          if(w > len -1){w = 0;}
                          var out = {},
                          group;
                          for (; i < len; i++) {
                              group = groups[i];
                              out[group.name] = Suma.apply(store, [group.children].concat(field));
                          }
                          var groupSum = out[groups[w].name];
                          w++;
                          return groupSum;
                      } else {
                          return Suma.apply(store, [records].concat(field));
                      }
                  },
                summaryRenderer: function(value, summaryData, dataIndex) {
                    return '<b>' + value + '</b>';
                }
            },
            {header: 'Aug', flex: 4, dataIndex: 'Aug',
                summaryType: function(field){
                      me = this;
                      var store = Ext.getStore('StoreBudgetGrid');
                      var records = store.data.items;
                      var field = ['Aug'];
                      function Suma(record, field) {
                          var total = 0;
                          var j = 0,
                          lenn = record.length;
                          for (; j < lenn; ++j) {
                             total = total + parseFloat(record[j].get(field));
                          }
                          return total.toFixed(2);
                      };
                      if (store.isGrouped()) {
                          var groups = store.getGroups();
                          var i = 0;
                          var len = groups.length;
                          if(w > len -1){w = 0;}
                          var out = {},
                          group;
                          for (; i < len; i++) {
                              group = groups[i];
                              out[group.name] = Suma.apply(store, [group.children].concat(field));
                          }
                          var groupSum = out[groups[w].name];
                          w++;
                          return groupSum;
                      } else {
                          return Suma.apply(store, [records].concat(field));
                      }
                  },
                summaryRenderer: function(value, summaryData, dataIndex) {
                    return '<b>' + value + '</b>';
                }
            },
            {header: 'Sep', flex: 4, dataIndex: 'Sep',
                summaryType: function(field){
                      me = this;
                      var store = Ext.getStore('StoreBudgetGrid');
                      var records = store.data.items;
                      var field = ['Sep'];
                      function Suma(record, field) {
                          var total = 0;
                          var j = 0,
                          lenn = record.length;
                          for (; j < lenn; ++j) {
                             total = total + parseFloat(record[j].get(field));
                          }
                          return total.toFixed(2);
                      };
                      if (store.isGrouped()) {
                          var groups = store.getGroups();
                          var i = 0;
                          var len = groups.length;
                          if(w > len -1){w = 0;}
                          var out = {},
                          group;
                          for (; i < len; i++) {
                              group = groups[i];
                              out[group.name] = Suma.apply(store, [group.children].concat(field));
                          }
                          var groupSum = out[groups[w].name];
                          w++;
                          return groupSum;
                      } else {
                          return Suma.apply(store, [records].concat(field));
                      }
                  },
                summaryRenderer: function(value, summaryData, dataIndex) {
                    return '<b>' + value + '</b>';
                }
            },
            {header: 'Oct', flex: 4, dataIndex: 'Oct',
                summaryType: function(field){
                      me = this;
                      var store = Ext.getStore('StoreBudgetGrid');
                      var records = store.data.items;
                      var field = ['Oct'];
                      function Suma(record, field) {
                          var total = 0;
                          var j = 0,
                          lenn = record.length;
                          for (; j < lenn; ++j) {
                             total = total + parseFloat(record[j].get(field));
                          }
                          return total.toFixed(2);
                      };
                      if (store.isGrouped()) {
                          var groups = store.getGroups();
                          var i = 0;
                          var len = groups.length;
                          if(w > len -1){w = 0;}
                          var out = {},
                          group;
                          for (; i < len; i++) {
                              group = groups[i];
                              out[group.name] = Suma.apply(store, [group.children].concat(field));
                          }
                          var groupSum = out[groups[w].name];
                          w++;
                          return groupSum;
                      } else {
                          return Suma.apply(store, [records].concat(field));
                      }
                  },
                summaryRenderer: function(value, summaryData, dataIndex) {
                    return '<b>' + value + '</b>';
                }
            },
            {header: 'Nov', flex: 4, dataIndex: 'Nov',
                summaryType: function(field){
                      me = this;
                      var store = Ext.getStore('StoreBudgetGrid');
                      var records = store.data.items;
                      var field = ['Nov'];
                      function Suma(record, field) {
                          var total = 0;
                          var j = 0,
                          lenn = record.length;
                          for (; j < lenn; ++j) {
                             total = total + parseFloat(record[j].get(field));
                          }
                          return total.toFixed(2);
                      };
                      if (store.isGrouped()) {
                          var groups = store.getGroups();
                          var i = 0;
                          var len = groups.length;
                          if(w > len -1){w = 0;}
                          var out = {},
                          group;
                          for (; i < len; i++) {
                              group = groups[i];
                              out[group.name] = Suma.apply(store, [group.children].concat(field));
                          }
                          var groupSum = out[groups[w].name];
                          w++;
                          return groupSum;
                      } else {
                          return Suma.apply(store, [records].concat(field));
                      }
                  },
                summaryRenderer: function(value, summaryData, dataIndex) {
                    return '<b>' + value + '</b>';
                }
            },
            {header: 'Dec', flex: 4, dataIndex: 'Dec',
                summaryType: function(field){
                      me = this;
                      var store = Ext.getStore('StoreBudgetGrid');
                      var records = store.data.items;
                      var field = ['Dec'];
                      function Suma(record, field) {
                          var total = 0;
                          var j = 0,
                          lenn = record.length;
                          for (; j < lenn; ++j) {
                             total = total + parseFloat(record[j].get(field));
                          }
                          return total.toFixed(2);
                      };
                      if (store.isGrouped()) {
                          var groups = store.getGroups();
                          var i = 0;
                          var len = groups.length;
                          if(w > len -1){w = 0;}
                          var out = {},
                          group;
                          for (; i < len; i++) {
                              group = groups[i];
                              out[group.name] = Suma.apply(store, [group.children].concat(field));
                          }
                          var groupSum = out[groups[w].name];
                          w++;
                          return groupSum;
                      } else {
                          return Suma.apply(store, [records].concat(field));
                      }
                  },
                summaryRenderer: function(value, summaryData, dataIndex) {
                    return '<b>' + value + '</b>';
                }
            },
            {header: 'Total', flex: 5, dataIndex: 'Total',
            summaryType: function(field){
                  me = this;
                  var store = Ext.getStore('StoreBudgetGrid');
                  var records = store.data.items;
                  var field = ['Total'];
                  function Suma(record, field) {
                      var total = 0;
                      var j = 0,
                      lenn = record.length;
                      for (; j < lenn; ++j) {
                         total = total + parseFloat(record[j].get(field));
                      }
                      return total.toFixed(2);
                  };
                  if (store.isGrouped()) {
                      var groups = store.getGroups();
                      var i = 0;
                      var len = groups.length;
                      if(w > len -1){w = 0;}
//                      console.log(w);
                      var out = {},
                      group;
                      for (; i < len; i++) {
                          group = groups[i];
                          out[group.name] = Suma.apply(store, [group.children].concat(field));
                      }
                      var groupSum = out[groups[w].name];
                      w++;
                      return groupSum;
                  } else {
                      return Suma.apply(store, [records].concat(field));
                  }
              },
            summaryRenderer: function(value, summaryData, dataIndex) {
                return '<b>' + value + '</b>';
            },
            field: {
                xtype: 'textfield',
                listeners: {
                    change: function(a,b,c) {
                        w = 0;
                        console.log(a.getValue());
                        console.log(Ext.getStore('StoreBudgetGrid').getModifiedRecords());
                    }
                }
            }
        }
        ],
        this.plugins = Ext.create('Ext.grid.plugin.CellEditing', {
                         clicksToEdit: 1
                     }),
        this.dockedItems = [{
            dock: 'top',
            xtype: 'toolbar',
            items: [{
                text: 'Show Summary',
                pressed: true,
                enableToggle: true,
                toggleHandler: function(btn, pressed){
                    w = 0;
                    var view = Ext.getCmp('budgetGrid').getView();
                    view.getFeature('group').toggleSummaryRow(pressed);
                    view.refresh();
                }
            },'-',{
                text: 'Propune un Buget',
                handler: function(btn, pressed){
                    w = 0;
                    Ext.Msg.alert('Status', 'A fost propus un buget.');
                }
            },'-',{
                text: 'Compara Bugetele',
                handler: function(btn, pressed){
                    w = 0;
                    Ext.Msg.alert('Status', 'A fost comparate bugetele.');
                }
            },'-',{
                text: 'Submit',
                handler: function(btn, pressed){
                    w = 0;
                    Ext.Msg.alert('Status', 'Au fost salvate.');
                    Ext.Ajax.request({
                        url: 'data/tools.php',
                        scope: this,
                        method: "GET",
                        params: {
                            storeTip: 'addRow',
                            acction: 'addrow',
                            denum: values.denum,
                            subpro: values.idsubpro,
                            cild: values.idcild,
                            data: Ext.Date.format(daa, 'Y-m-d'),
                            suma: values.suma,
                            UserID: UserID
                            },
                        success: function(response){
                            var obj = Ext.decode(response.responseText);
                            var responseData = obj.rows[0];
                        }
                    });
                }
            }]
        }],
        this.features = [{
            id: 'group',
            groupHeaderTpl: 'Subject: {name}',
            ftype: 'groupingsummary',
            hideGroupedHeader: true,
            enableGroupingMenu: false
        }],
//        this.viewConfig = {
//            stripeRows: false
//        },
        this.callParent(arguments);
	},

});